import Vue from 'vue'
import Router from 'vue-router'

const staffManage = r =>
  require.ensure([], () => r(require('@/views/members/member/staffManage')), 'staffManage')

const staffDetail = r =>
  require.ensure([], () => r(require('@/views/members/member/staffDetail')), 'staffDetail')

const login = r =>
  require.ensure([], () => r(require('@/views/login/index')), 'login')

const purchaseRec = r =>
  require.ensure([], () => r(require('@/views/members/drugUpload/purchaseRec')), 'purchaseRec')

const inventoryPresent = r =>
  require.ensure([], () => r(require('@/views/members/drugUpload/inventoryPresent')), 'inventoryPresent')

const wholeCheckout = r =>
  require.ensure([], () => r(require('@/views/members/drugUpload/wholeCheckout')), 'wholeCheckout')

const goods = r =>
  require.ensure([], () => r(require('@/views/members/dataMatch/goods')), 'goods')

const business = r =>
  require.ensure([], () => r(require('@/views/members/dataMatch/business')), 'business')

const goodsWarning = r =>
  require.ensure([], () => r(require('@/views/members/erpHomePage/goodsWarning')), 'goodsWarning')

const saleDetail = r =>
  require.ensure([], () => r(require('@/views/members/erpHomePage/saleDetail')), 'saleDetail')

const dc = r =>
  require.ensure([], () => r(require('@/views/baseSettings/dc/index')), 'dc')

const functionPer = r =>
  require.ensure([], () => r(require('@/views/members/erpSetting/functionPer')), 'functionPer')

const system = r =>
  require.ensure([], () => r(require('@/views/members/backUpData/system')), 'system')

const notFound = r =>
  require.ensure([], () => r(require('@/views/404')), 'notFound')

// 解决Uncaught (in promise) NavigationDuplicated
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location)
}

const PROJECT_NAME = 'yos-web/' // 项目名
const DELETE_LENGTH = 8 // history模式下正式服相对根目录的地址要截取的长度

Vue.use(Router)

import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    name: 'login',
    component: login,
    hidden: true
  }
]
export const asyncRoutes = [
  {
    path: '/',
    name: '/',
    component: Layout,
    redirect: '/dashboard',
    meta: { title: '初始化', icon: 'dashboard' },
    children: [
      {

        path: 'dashboard',
        name: 'dashboard',
        component: staffManage,
        meta: { title: '人员设置', icon: 'clipboard' }
      },
      {
        path: 'staffDetail/:billNo',
        name: 'staffDetail',
        component: staffDetail,
        meta: { title: '编辑', icon: 'clipboard' },
        hidden: true
      }
    ]
  },
  {
    path: '/drugUpload',
    name: 'drugUpload',
    component: Layout,
    redirect: '/drugUpload/purchaseRec',
    meta: { title: '药监上传', icon: 'dashboard' },
    children: [
      {
        path: 'purchaseRec',
        name: 'purchaseRec',
        component: purchaseRec,
        meta: { title: '采购入库', icon: 'clipboard' }
      },
      {
        path: 'inventory',
        name: 'inventory',
        component: inventoryPresent,
        meta: { title: '库存现有量', icon: 'clipboard' }
      },
      {
        path: 'wholeCheckout',
        name: 'wholeCheckout',
        component: wholeCheckout,
        meta: { title: '销售出库', icon: 'clipboard' }
      }
    ]
  },
  {
    path: '/dataMatch',
    name: 'dataMatch',
    component: Layout,
    redirect: '/dataMatch/goods',
    meta: { title: '数据匹配', icon: 'dashboard' },
    children: [
      {
        path: 'goods',
        name: 'goods',
        component: goods,
        meta: { title: '商品', icon: 'clipboard' }
      },
      {
        path: 'business',
        name: 'business',
        component: business,
        meta: { title: '供应商', icon: 'clipboard' }
      }
    ]
  },
  {
    path: '/baseSettings',
    name: 'baseSettings',
    alwaysShow: true,
    component: Layout,
    redirect: '/baseSettings/dc',
    meta: { title: '基础配置', icon: 'clipboard' },
    children: [
      {
        path: 'dc',
        name: 'dc',
        component: dc,
        meta: { title: '数字证书设置' }
      }
    ]
  },
  {
    path: '/erpHome',
    name: 'erpHome',
    component: Layout,
    redirect: '/saleDetail',
    meta: { title: 'erp首页', icon: 'dashboard' },
    hidden: true,
    children: [
      {
        path: '/saleDetail',
        name: 'saleDetail',
        component: saleDetail,
        meta: { title: '销售概况' },
        hidden: true
      },
      {
        path: '/goodsWarning',
        name: 'goodsWarning',
        component: goodsWarning,
        meta: { title: '预警' },
        hidden: true
      }
    ]
  },
  {
    path: '/erpSetting',
    name: 'erpSetting',
    alwaysShow: true,
    component: Layout,
    redirect: '/erpSetting',
    meta: { title: '系统设置', icon: 'clipboard' },
    children: [
      {
        path: '/functionPer',
        name: 'functionPer',
        component: functionPer,
        meta: { title: '功能权限' }
      },
      {
        path: '/system',
        name: 'system',
        component: system,
        meta: { title: '数据备份导出' }
      }
    ]
  },
  {
    path: '/404',
    name: '404',
    component: notFound,
    hidden: true
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: { name: '404' }, hidden: true }
]

const createRouter = () => new Router(Object.assign(
  {
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  },
  {
    mode: 'history', // require service support
    base: getAbsolutePath()
  }))

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

function getAbsolutePath() {
  const path = location.pathname
  return path.substring(0, path.lastIndexOf(PROJECT_NAME) + DELETE_LENGTH)
}

export default router
